import { fileURLToPath, URL } from 'node:url'
import { join } from 'node:path'
import { defineConfig,loadEnv } from 'vite'
import type { UserConfig } from 'vite'
import baseConfig,{loadEnvToConfig} from "./vite.base.config"

const projectId = 'tenant'

const root = fileURLToPath(new URL('./', import.meta.url))

// https://cn.vitejs.dev/config/
// { command, mode, isSsrBuild, isPreview }
const alias = {
    ...baseConfig.resolve?.alias,
    '@app': join(root, `src/app/${projectId}`)
}

export default defineConfig(({ mode }) => {
    const env = loadEnv(projectId, root, '')
    let config:UserConfig = baseConfig;
    loadEnvToConfig(config, env);
    config.base = `/ui/${projectId}/`
    config.build = {...config.build, outDir:`dist/${projectId}`}
    config.define = {
        __APPID__: JSON.stringify(projectId),
        'import.meta.env.CONF_API_URL': JSON.stringify(env.CONF_API_URL)
    }
    config.resolve = {
        ...config.resolve,
        alias
    }
    return config
})